import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.*; //package DOM
import org.xml.sax.SAXException;

import javax.xml.parsers.*; 
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;



public class question3 {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		DocumentBuilder db = dbf.newDocumentBuilder ();
		
		Document doc_comp = db.parse(new File("Competences.xml"));
		
		ArrayList<String> test = listeCv();
		for(int i = 0; i < test.size(); i++) {
			System.out.println(test.get(i));
			Document doc_cv = db.parse(new File(test.get(i)));

			doc_comp = cv_comp(doc_cv, doc_comp);
		}


		transformerXml(doc_comp, "Competences.xml");
		

	}
	
	
	public static void transformerXml(Document document, String fichier) {
		try {
			// Création de la source DOM
			Source source = new DOMSource(document);
			
			// Création du fichier de sortie
			StreamResult resultat = new StreamResult(fichier);
			
			// Configuration du transformer
			TransformerFactory fabrique = TransformerFactory.newInstance();
			Transformer transformer = fabrique.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
			
			// Transformation
			transformer.transform(source, resultat);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static boolean contain(NodeList p_list, Node node) {
		for(int i = 0; i < p_list.getLength(); i++) {
			if(node.getTextContent().trim().toUpperCase().equals(p_list.item(i).getTextContent().trim().toUpperCase()))
				return true;
		}
		
		return false;
	}
	
	public static Document cv_comp(Document doc_cv, Document doc_comp) {
		Element racine_comp = doc_comp.getDocumentElement();
		Element racine_cv = doc_cv.getDocumentElement();

		NodeList noms_com = racine_comp.getElementsByTagName("Nom");
		

		NodeList comps_cv = racine_cv.getElementsByTagName("Competences");
		comps_cv = comps_cv.item(0).getChildNodes();
		

		for(int i = 1; i < comps_cv.getLength(); i += 2) {
			if(!contain(noms_com, comps_cv.item(i))) {				
				
				Element competence = doc_comp.createElement("Competence");
				competence.setAttribute("idC", "C" + (racine_comp.getElementsByTagName("Nom").getLength() + 1));
				
				Element nom = doc_comp.createElement("Nom");
				Element type = doc_comp.createElement("Type");

				nom.appendChild(doc_comp.createTextNode(comps_cv.item(i).getTextContent().trim()));
				type.appendChild(doc_comp.createTextNode(comps_cv.item(i).getAttributes().getNamedItem("type").getNodeValue().trim()));

				competence.appendChild(nom);
				competence.appendChild(type);
				
				racine_comp.appendChild(competence);
			}
		}
		
		return doc_comp;
	}
	
	public static ArrayList<String> listeCv(){

		Pattern p = Pattern.compile("P[0-9]{8}.xml");
		String [] s = new File(".").list();
		ArrayList<String> listeFichiers = new ArrayList<String>();
		
		for (int i=0; i<s.length;i++) {
			Matcher m = p.matcher(s[i]);
			if ( m.matches())
				listeFichiers.add(s[i]);
		}
		
		return listeFichiers;
	}

}
